package com.example.jiaoyidui.component;

import com.alibaba.excel.EasyExcel;
import com.example.jiaoyidui.entity.ApiRequestParam;
import com.example.jiaoyidui.entity.excel.TransPairExcel;
import com.example.jiaoyidui.listener.ExcelListener;
import com.example.jiaoyidui.util.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class Handler {

    public void begin(int times, String chainCode) {
        init();
        List<ApiRequestParam> list = null;
        switch (chainCode) {
            case "BSC":
                list = SystemProperties.bscList;
                break;
            case "Polygon":
                list = SystemProperties.polygonList;
                break;
            case "Avalanche":
                list = SystemProperties.avalancheList;
                break;
            case "Fantom":
                list = SystemProperties.fantomList;
                break;
            case "ETH":
                list = SystemProperties.ethList;
                break;
        }
        for (int i = 0; i < times;i++) {

            new Task().task(chainCode, list);
            try {
                Thread.sleep(120000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new Handler().init();
        //new Task().task("bsc", SystemProperties.bscList);
        //new Task().task("polygon", SystemProperties.polygonList);
        //new Task().task("avalanche", SystemProperties.avalancheList);
        //new Task().task("fantom", SystemProperties.fantomList);
        //new Task().task("eth", SystemProperties.ethList);

        for (int i = 0; i < 10;i++) {
            new Task().task("eth", SystemProperties.ethList);
            //new Task().task("bsc", SystemProperties.bscList);
            //new Task().task("polygon", SystemProperties.polygonList);
            //new Task().task("avalanche", SystemProperties.avalancheList);
            try {
                Thread.sleep(120000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 每30分钟执行一次
     */
//    @PostConstruct // 启动时执行一次
    //@Scheduled(cron = "0 0/30 * * * ?")
    public void handler() {
        // 初始化，从excel读入交易对
        init();

        // 开始bsc和polygon任务
        ThreadPoolUtil.getThreadPool().execute(() -> new Task().task("bsc", SystemProperties.bscList));
        ThreadPoolUtil.getThreadPool().execute(() -> new Task().task("polygon", SystemProperties.polygonList));
        ThreadPoolUtil.getThreadPool().execute(() -> new Task().task("avalanche", SystemProperties.avalancheList));
        ThreadPoolUtil.getThreadPool().execute(() -> new Task().task("fantom", SystemProperties.fantomList));
        ThreadPoolUtil.getThreadPool().execute(() -> new Task().task("eth", SystemProperties.ethList));
    }

    /**
     * 初始化，从excel读入交易对
     */
    private void init() {
        SystemProperties.allTransPairList.clear();
        SystemProperties.bscList.clear();
        SystemProperties.polygonList.clear();
        SystemProperties.avalancheList.clear();
        SystemProperties.fantomList.clear();
        SystemProperties.ethList.clear();

        String readPath = SystemProperties.USER_DIR + SystemProperties.READ_PATH;
        EasyExcel.read(readPath, TransPairExcel.class, new ExcelListener())
                .sheet(0) // Starting from 0
                .doRead();
        EasyExcel.read(readPath, TransPairExcel.class, new ExcelListener())
                .sheet(1) // Starting from 0
                .doRead();
        EasyExcel.read(readPath, TransPairExcel.class, new ExcelListener())
                .sheet(2) // Starting from 0
                .doRead();
        EasyExcel.read(readPath, TransPairExcel.class, new ExcelListener())
                .sheet(3) // Starting from 0
                .doRead();
        EasyExcel.read(readPath, TransPairExcel.class, new ExcelListener())
                .sheet(4) // Starting from 0
                .doRead();
    }
}
